473,418 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Need help accessing radio button in function ...

I have a function that changes which radio button is selected. I need to pass the form name to the
function but not the field name and am doing so like:

changeRadio(this.form);

The function:

changeRadio(formname){

// I have skipped how the variable fieldname is created all that is important is that it needs to be
created in the function, not passed to it, thus the need (I think) for the following

var radio2check = eval(formname+'.'+fieldname); // Don't work
var radio2check = eval("document.actualformname."+fieldname); // Works

// later in the function the above is used like:

radio2check[0].checked = true;

}

So in other words, when I manually enter the form name in the eval part it works fine but when I use
the formname containing this.form passed into the function it don't work. I need to know how to
access the radio button by combining the formname passed into the function and the fieldname
variable created in the function.
Nov 5 '05 #1
6 1841
I tried changing

var radio2check = eval(formname+'.'+fieldname);

to

var radio2check = eval('document.'+formname.name+'.'+fieldname);

and it appears to work. If anyone has a better solution, please let me know.

Nov 5 '05 #2
J Mox wrote:
I tried changing

var radio2check = eval(formname+'.'+fieldname);

to

var radio2check = eval('document.'+formname.name+'.'+fieldname);
Eval is almost never, ever needed - follow the link from the FAQ on
square bracket notation:

<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>
A guess at what you might need is:

var radio2check = document.forms[formname].elements[fieldname];


and it appears to work. If anyone has a better solution, please let me know.


Your eval method may work, but it is less than optimal.
--
Rob
Nov 5 '05 #3
J Mox wrote:
I tried changing

var radio2check = eval(formname+'.'+fieldname);

to

var radio2check = eval('document.'+formname.name+'.'+fieldname);

and it appears to work. If anyone has a better solution,
please let me know.


If the latter "works" then the variable (paramerter?) - formname - is
not a string representation of a name but an object that has a property
called - name -. It is also an object with the property - name - where
that name happens to be the name of a FORM Element object. That is
unlikely to be a coincidence and so I would deduce that - formname - is
a reference to an object that is a FORM element object, indeed it is
_the_ form element object. So some confusion has been introduced by
giving it a name that actually conceals its real nature.

If you do - eval('document.'+formRef.name); - what you get back is -
formRef -, a reference to the FORM object that you started with, so you
can skip that. if you do - eval('fromRef.'+fieldName) - you are doing
the same as - formRef[fieldname] - but slower and more indirectly.

var radio2check = formname[fieldname];

- should "work" at least as effectively as the - eval - and is; shorter,
simpler, faster and more direct (and so easier to debug and maintain).
(but do change the - formname - variable name)

A general rule for newcomers to javascript would be that if you are
considering using - eval - then you have the opportunity to learn
something new that will not use - eval - and be objectively better than
the - eval - use.

Richard.
Nov 5 '05 #4
"RobG" <rg***@iinet.net.au> wrote in message
news:43**********************@per-qv1-newsreader-01.iinet.net.au...
J Mox wrote:
I tried changing

var radio2check = eval(formname+'.'+fieldname);

to

var radio2check = eval('document.'+formname.name+'.'+fieldname);


Eval is almost never, ever needed - follow the link from the FAQ on square bracket notation:

<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>
A guess at what you might need is:

var radio2check = document.forms[formname].elements[fieldname];


and it appears to work. If anyone has a better solution, please let me know.


Your eval method may work, but it is less than optimal.
--
Rob


Thanks for the link. If I was passing the actual form name as a string, which is what I mistakenly
thought I was in effect doing when I passed this.form to the function, then I think your solution
would work but since I am passing what I have now come to understand is a object referencing a form
the following works.

var radio2check = formname[fieldname];
Nov 7 '05 #5
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:dk*******************@news.demon.co.uk...
J Mox wrote:
I tried changing

var radio2check = eval(formname+'.'+fieldname);

to

var radio2check = eval('document.'+formname.name+'.'+fieldname);

and it appears to work. If anyone has a better solution,
please let me know.


If the latter "works" then the variable (paramerter?) - formname - is
not a string representation of a name but an object that has a property
called - name -. It is also an object with the property - name - where
that name happens to be the name of a FORM Element object. That is
unlikely to be a coincidence and so I would deduce that - formname - is
a reference to an object that is a FORM element object, indeed it is
_the_ form element object. So some confusion has been introduced by
giving it a name that actually conceals its real nature.

If you do - eval('document.'+formRef.name); - what you get back is -
formRef -, a reference to the FORM object that you started with, so you
can skip that. if you do - eval('fromRef.'+fieldName) - you are doing
the same as - formRef[fieldname] - but slower and more indirectly.

var radio2check = formname[fieldname];

- should "work" at least as effectively as the - eval - and is; shorter,
simpler, faster and more direct (and so easier to debug and maintain).
(but do change the - formname - variable name)

A general rule for newcomers to javascript would be that if you are
considering using - eval - then you have the opportunity to learn
something new that will not use - eval - and be objectively better than
the - eval - use.

Richard.


Thanks, you helped me understand the difference between objects and properties. I was passing
this.form to the function as formname (I now see and agree how the variable was confusingly named)
mistakenly thinking that it resulted in the actual form name being passed to the function. I think I
now understand that passing this.form to a function passes a form element object from which form
properties can be accessed such as your simpler example which worked fine.
Nov 7 '05 #6
J Mox wrote:
[...]

Thanks for the link. If I was passing the actual form name as a string, which is what I mistakenly
thought I was in effect doing when I passed this.form to the function, then I think your solution
would work but since I am passing what I have now come to understand is a object referencing a form
the following works.

var radio2check = formname[fieldname];


You are passing a reference to the form object.

Glad to help. :-)
--
Rob
Nov 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jason | last post by:
The following (likely far from imperfect code), reports a value of NaN in the j4 display. I suppose the problem is I am not really passing the "checked" value of the radio button via .value ......
1
by: MickG | last post by:
I am trying to change the value of the variable "hard" according to which radio button is pressed and I am having no joy. Could anyone help me with this, the problematic section is marked with...
5
by: Digital Puer | last post by:
I have the following HTML form: - radio button A (default selected) - radio button B - input field, of type "file" with "Choose" button - submit button I would like to have it so that if the...
2
by: Steve Black | last post by:
Hello, I am dynamically creating checkboxes on my web page based on data in a SQL Server table. Therefore, these checkboxes do not exist at design time and I cannot refer to them in my...
3
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
9
by: IchBin | last post by:
I can not see what the problem is with this script. I am just trying to set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry I am new to javascript. <html> <head> <script...
7
by: Franky | last post by:
Following discussion in a previous post, i have used an array inside a loop to gather results: //Function called from another page function displayQuestions($ModNum){ //modnum is used...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.